<template>
  <div class="title">超级管理员登录页面</div>
  <div class="login">
    <div class="login_container">
      <div class="login_box">
        <!-- 头像区域 -->
        <div class="avatar_box">
          <img src="../../assets/image/logo.png" alt="" />
        </div>
        <!-- 登录表单区域 -->
        <el-form
          ref="loginFormRef"
          :model="loginForm"
          label-width="0px"
          class="login_form"
          :rules="rules"
        >
          <!-- 用户名 -->
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              style="width: 380px; height: 50px; margin-left: 15px"
              placeholder="账号"
              class="account"
            />
          </el-form-item>
          <!-- 密码 -->
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              style="width: 380px; height: 50px; margin-left: 15px"
              type="password"
              placeholder="密码"
              show-password
            />
          </el-form-item>
          <!-- 按钮区域 -->
          <el-form-item class="btns">
            <el-button
              type="primary"
              style="width: 380px; height: 50px; margin-left: 15px"
              @click="userLogin"
            >
              超级管理员登录
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
  <div class="bg">
    <Particles
      id="tsparticles"
      :particlesInit="particlesInit"
      :particlesLoaded="particlesLoaded"
      :options="options"
    />
  </div>
  <div class="change">
    <span>切换登录方式</span>
    &nbsp;
    <el-icon @click="goUserlogin"><Avatar /></el-icon>
    &nbsp;
    <el-icon @click="goAdminlogin"><UserFilled /></el-icon>
    &nbsp;
    <el-icon><Platform /></el-icon>
  </div>
</template>

<script setup>
import { reactive } from "vue";
import { loadFull } from "tsparticles";
import { useRouter } from "vue-router";
// import type { Engine } from "tsparticles-engine";
import { SuperAdminLogin } from "../../api/api";
import { userStore } from "../../store/UserStore";

//用户token
const useStore = userStore();
const router = useRouter();
//登录表单
const loginForm = reactive({
  username: "zzn",
  password: "123456",
});

//表单规则
const rules = reactive({
  username: [
    {
      required: true,
      message: "用户名不能为空",
      trigger: "blur",
    },
    {
      min: 3,
      max: 20,
      message: "长度在 3 到 20 个字符",
      trigger: "blur",
    },
  ],
  password: [
    { required: true, message: "密码不能为空", trigger: "blur" },
    {
      min: 6,
      max: 15,
      message: "长度在 6 到 15 个字符",
      trigger: "blur",
    },
  ],
});

const userLogin = () => {
  SuperAdminLogin(loginForm)
    .then(res => {
      console.log(res.data.accessToken, "111");
      useStore.setuserStore(res.data.accessToken);
      router.push({ name: "SuperHome" });
      ElMessage({
        message: "登录成功",
        type: "success",
      });
    })
    .catch(err => {
      console.log(err, "err");
      ElMessage.error("网络错误、请重新登录");
    });
};

const goAdminlogin = () => {
  router.push({ name: "Adminlogin" });
};

const goUserlogin = () => {
  router.push({ name: "Userlogin" });
};

//粒子特效
//--------------------------------------------------------------------------
const particlesInit = async engine => {
  await loadFull(engine);
};

const particlesLoaded = async container => {
  // console.log("Particles container loaded", container);
};
const options = reactive({
  background: {
    color: {
      value: "#F8F9FA", // 粒子颜色
    },
  },
  fpsLimit: 60,
  interactivity: {
    events: {
      onClick: {
        enable: true,
        mode: "push", // 可用的click模式有: "push", "remove", "repulse", "bubble"。
      },
      onHover: {
        enable: true,
        mode: "grab", // 可用的hover模式有: "grab", "repulse", "bubble"。
      },
      resize: true,
    },
    modes: {
      bubble: {
        distance: 400,
        duration: 2,
        opacity: 0.8,
        size: 40,
      },
      push: {
        quantity: 4,
      },
      repulse: {
        distance: 200,
        duration: 0.4,
      },
    },
  },
  particles: {
    color: {
      value: "#50D05D",
    },
    links: {
      color: "#11BA73", // '#dedede'。线条颜色。
      distance: 150, // 线条长度
      enable: true, // 是否有线条
      opacity: 0.5, // 线条透明度。
      width: 1, // 线条宽度。
    },
    collisions: {
      enable: false,
    },
    move: {
      direction: "none",
      enable: true,
      outMode: "bounce",
      random: false,
      speed: 4, // 粒子运动速度。
      straight: false,
    },
    number: {
      density: {
        enable: true,
        area: 1000,
      },
      value: 80, // 粒子数量。
    },
    opacity: {
      value: 0.4, // 粒子透明度。
    },
    shape: {
      type: "circle", // 可用的粒子外观类型有："circle","edge","triangle", "polygon","star"
    },
    size: {
      random: true,
      value: 5,
    },
  },
  detectRetina: true,
});
</script>

<style lang="less" scoped>
.login {
  z-index: 99999;
  position: absolute;
  left: 50%;
  top: 410px;
}

.login_box {
  height: 300px;
  width: 450px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);

  .avatar_box {
    width: 100px;
    height: 100px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;

    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}

.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}

.btns {
  display: flex;
  justify-content: flex-end;
}
.change {
  font-size: 30px;
  position: absolute;
  right: 30px;
  bottom: 20px;
  color: #409eff;
}
.title {
  z-index: 20;
  font-size: 30px;
  position: absolute;
  top: 130px;
  left: 42%;
  color: rgb(0, 0, 0);
}
</style>
